home *** CD-ROM | disk | FTP | other *** search
/ MacFormat 1994 November / macformat-018.iso / Utility Spectacular / Developer / macgzip_022-src / macos / think / MacGzip.c < prev    next >
Encoding:
C/C++ Source or Header  |  1994-07-26  |  8.8 KB  |  465 lines  |  [TEXT/KAHL]

  1. /*
  2.  * Copyright (C) 1993  SPDsoft
  3.  * 
  4.  */
  5.  
  6. #include <GestaltEqu.h>
  7. #include <Sound.h>
  8. /*#include <EPPC.h>*/
  9.  
  10. #include "MacGzip.h"
  11. #include "ThePrefs.h"
  12. #include "MacErrors.h"
  13. #include "SPDProg.h"
  14.  
  15. #include <string.h>
  16.  
  17. void    about(void);
  18. void    InitTheMac(void);
  19. int        OpenFromFinder( void );
  20. void    MyBeep(void);
  21.  
  22. void CenterPicRect(PicHandle thePic, Rect *inRect, Rect *outRect);
  23.  
  24.  
  25. static    pascal char
  26.         MyFilter ( DialogPtr theDialog, EventRecord *theEvent,short  *itemHit );
  27.  
  28. extern OSErr
  29.         DoOpen( Str255 fName, long vRefNum);
  30.  
  31. /* CONST */
  32. #define NIL 0L
  33.  
  34. static Point    myPoint =    {80,80};
  35.  
  36. Boolean            quitting = false;
  37.  
  38. MenuHandle        myMenus[4];
  39. OSErr            theOSErr;
  40. KeyMap            theKeys;
  41. char            charKey;
  42. Boolean            modKey;
  43.  
  44.  
  45. void InitTheMac(void)
  46. {
  47. long gestAliasResponse, gestStdFileResponse, gestFSResponse;
  48.  
  49.     FlushEvents(everyEvent, 0);
  50.     InitGraf(&qd.thePort);
  51.     InitFonts();
  52.     InitWindows();
  53.     InitMenus();
  54.     TEInit();
  55.     InitDialogs(NIL);
  56.     InitCursor();
  57.     MaxApplZone();
  58.  
  59.     GetKeys(&theKeys);
  60.  
  61. /* sys 7 ?*/
  62.  
  63.     theOSErr = Gestalt(gestaltAliasMgrAttr, &gestAliasResponse);
  64.     if (theOSErr == noErr) theOSErr = Gestalt(gestaltFSAttr, &gestFSResponse);
  65.     if (theOSErr == noErr)
  66.         theOSErr = Gestalt(gestaltStandardFileAttr, &gestStdFileResponse);
  67.     
  68.     if (theOSErr != noErr ||
  69.         (gestAliasResponse & (1 << gestaltAliasMgrPresent)) == 0 ||
  70.         (gestFSResponse & (1 << gestaltHasFSSpecCalls)) == 0 ||
  71.         (gestStdFileResponse & (1 << gestaltStandardFile58)) == 0) {
  72.     
  73.         theAlert( OLD_SYSTEM,GENERIC, theOSErr, true );
  74.     }
  75.     
  76.     CanBreak=true;
  77.  
  78.     if (!GetOrGeneratePrefs(&currPrefs, kCurrentPrefsTypeVers))
  79.     {
  80.         about();
  81.         prefsChangedFlag = DoPrefsDialog(&currPrefs);
  82.         if (prefsChangedFlag)
  83.         {
  84.             if( ( theOSErr = SavePrefs(&currPrefs)) != noErr )
  85.                     theAlert(NO_SAVE_PREF,GENERIC,theOSErr, true );
  86.         }
  87.         else ExitToShell();
  88.     }
  89.  
  90. }
  91.  
  92. main()
  93.  
  94. { /* main program */
  95.     extern    Boolean        gSavedPos;
  96.     extern    Point        gSavedPoint;
  97.     
  98.     extern Handle    FPrefs;
  99.     extern Size        FPrefsSize;
  100.  
  101.  
  102.     InitTheMac();
  103.     SetUpMenus();
  104.     
  105.     if ( 0!=OpenFromFinder() )
  106.     {
  107.         quitting=true;
  108.     }
  109.     
  110.     while( !quitting )
  111.     {
  112.         MainEvent();
  113.     }
  114.  
  115. /* remember window pos */
  116.             
  117.     currPrefs.SavedPos = gSavedPos; /* always true */
  118.         
  119.     if(( gSavedPoint.h != currPrefs.SavedPoint.h ) ||
  120.         ( gSavedPoint.v != currPrefs.SavedPoint.v ) )
  121.     {
  122.         currPrefs.SavedPoint.h = gSavedPoint.h;
  123.         currPrefs.SavedPoint.v = gSavedPoint.v;
  124.         prefsChangedFlag=true;
  125.     }
  126.  
  127.     if (prefsChangedFlag)
  128.     {
  129.         if( ( theOSErr = SavePrefs(&currPrefs)) != noErr )
  130.                 theAlert(NO_SAVE_PREF,GENERIC,theOSErr, true );
  131.     }
  132.     
  133.     if( FPrefsSize != 0 ) DisposeHandle( FPrefs );
  134.     ExitToShell();
  135. }
  136.  
  137.  
  138. void MainEvent(void)
  139. {
  140.     EventRecord myEvent;
  141.     WindowPtr whichWindow;
  142.     short windowPart;
  143.  
  144.     SystemTask();
  145.     if( GetNextEvent(everyEvent, &myEvent) ) {
  146.         switch( myEvent.what ) {
  147.             case mouseDown:
  148.                 windowPart = FindWindow(myEvent.where, &whichWindow);
  149.                 DoMouseDown(windowPart, whichWindow, &myEvent);
  150.                 break;
  151.                 
  152.             case keyDown:
  153.             case autoKey: 
  154.             {
  155.                 register char theChar;
  156.     
  157.                 theChar = myEvent.message & charCodeMask;
  158.                 if ((myEvent.modifiers & cmdKey) != 0) 
  159.                     DoCommand( MenuKey(theChar) );
  160.                                     
  161.                 break;
  162.             }
  163.                 
  164.             case activateEvt:
  165.                 break;
  166.  
  167.             case updateEvt: 
  168.                 break;
  169.                 
  170.             case diskEvt:
  171.             if ((myEvent.message >> 16) != noErr)
  172.             {
  173.                 theOSErr = DIBadMount(myPoint, myEvent.message);
  174.                 theAlert(BAD_DISK,GENERIC,theOSErr,false);
  175.             }
  176.             break;
  177.  
  178.             case osEvt:
  179.             switch ((myEvent.message >> 24) & 0x0ff)
  180.             {
  181.                 case suspendResumeMessage:
  182.                     
  183.                     SetCursor(&qd.arrow);
  184.                 
  185.                     if ((myEvent.message & resumeFlag) == 0) // suspend 
  186.                     {
  187.                     }
  188.                     else
  189.                     {                                        // resume
  190.                     }
  191.                     break;
  192.                     
  193.                 case mouseMovedMessage:
  194.                     break;
  195.             }
  196.             break;
  197.                 
  198. /*            case kHighLevelEvent:*/
  199. /*                break;*/
  200.  
  201.  
  202.         }
  203.     }
  204. }
  205. void DoCommand(long mResult)
  206. {
  207.     short    theItem;
  208.     Str255    name;
  209.  
  210.     theItem = LoWord(mResult);
  211.     switch( HiWord(mResult) ) {
  212.         case appleID:
  213.             if( theItem > 2 ) {
  214.                 CGrafPtr savePort;
  215.                 GetItem(myMenus[appleM], theItem, &name);
  216.                 GetPort(&savePort);
  217.                 OpenDeskAcc(name);
  218.                 SetPort(savePort);
  219.             }
  220.             else
  221.             {
  222.                 about();
  223.             }
  224.             break;
  225.  
  226.         case fileID:
  227.             DoFile(theItem);
  228.             break;
  229.  
  230.         case editID:
  231.             SystemEdit(theItem-1);
  232.             break;
  233.             
  234.         case gzipID:
  235.         
  236.             switch(theItem){
  237.             
  238.             case gmAscii:
  239.             
  240.                 currPrefs.ascii=true;
  241.                 CheckItem( myMenus[gzipM], gmBin, false );
  242.                 CheckItem( myMenus[gzipM], gmAscii, true);
  243.                 break;
  244.  
  245.             case gmBin:
  246.             
  247.                 currPrefs.ascii=false;
  248.                 CheckItem( myMenus[gzipM], gmAscii , false );
  249.                 CheckItem( myMenus[gzipM], gmBin, true);
  250.                 break;
  251.             
  252.             case gmComp:
  253.             
  254.                 currPrefs.compress=true;
  255.                 CheckItem( myMenus[gzipM], gmUncomp, false );
  256.                 CheckItem( myMenus[gzipM], gmComp, true);
  257.                 break;
  258.  
  259.             case gmUncomp:
  260.             
  261.                 currPrefs.compress=false;
  262.                 CheckItem( myMenus[gzipM], gmComp , false );
  263.                 CheckItem( myMenus[gzipM], gmUncomp, true);
  264.                 break;
  265.             
  266.             }
  267.             prefsChangedFlag = true;
  268.             break;
  269.     }
  270.  
  271.     HiliteMenu(0);
  272. }
  273.  
  274. void DoFile(short item)
  275. {
  276. static SFReply    reply;
  277. SFTypeList        typeList =    {'Gzip','ZIVU','pZIP','ZIVM'}; 
  278.  
  279.     switch( item ) {
  280.         case fmOpen:
  281.         
  282.             SFGetFile(
  283.                         myPoint,
  284.                         "\p",
  285.                         0L,
  286.                         -1,
  287. /*                        currPrefs.compress ? -1:4,*/
  288.                         typeList,
  289.                         0L,
  290.                         &reply
  291.                     );
  292.             
  293.             if( reply.good != false )
  294.             {
  295.                 if( DoOpen( reply.fName, reply.vRefNum )!=128)
  296.                 {
  297.                     if(currPrefs.BeepWhenDone) MyBeep();
  298.                     if(currPrefs.QuitWhenDone) quitting=true;
  299.                 }
  300.             }
  301.             break;
  302.  
  303.         case fmPrefs:
  304.         
  305.             prefsChangedFlag = DoPrefsDialog(&currPrefs);
  306.             if (prefsChangedFlag)
  307.             {
  308.                 if( ( theOSErr = SavePrefs(&currPrefs)) != noErr )
  309.                         theAlert(NO_SAVE_PREF,GENERIC,theOSErr, true );
  310.             }
  311.             break;
  312.  
  313.         case fmQuit:
  314.         
  315.             quitting = true;
  316.             break;
  317.     }
  318. }
  319. void DoMouseDown(short windowPart, WindowPtr whichWindow, EventRecord *myEvent)
  320. {
  321.     switch( windowPart ) {
  322.         case inGoAway:
  323.             break;
  324.  
  325.         case inMenuBar:
  326.             DoCommand( MenuSelect(myEvent->where) );
  327.             break;
  328.  
  329.         case inSysWindow:
  330.             SystemClick(myEvent, whichWindow);
  331.             break;
  332.  
  333.         case inDrag:
  334.             break;
  335.  
  336.         case inGrow:
  337.             break;
  338.  
  339.         case inContent:
  340.             if( whichWindow != FrontWindow() )
  341.                 SelectWindow(whichWindow);
  342.             break;
  343.     }
  344. }
  345.  
  346. int OpenFromFinder( void )
  347. {
  348.     int    message, count, index;
  349.     AppFile        theFile;
  350.     Boolean        HasToBeep=false;
  351.     
  352.     modKey = (( IsOptKey(theKeys) ) ? true : false);
  353.     
  354.     if (IsAKey(theKeys)) charKey = 'a';
  355.     else if (IsBKey(theKeys)) charKey = 'b';
  356.     else charKey = 0x00;
  357.     
  358.     CountAppFiles( &message, &count);            /*any files to open?*/
  359.     for( index= 1 ; index <= count ; index ++)
  360.     {
  361.         GetAppFiles(index, &theFile);
  362.         
  363.         if( DoOpen(theFile.fName, theFile.vRefNum)!=128)
  364.         {
  365.             HasToBeep=currPrefs.BeepWhenDone;
  366.         }
  367.     }
  368.     
  369.     if(HasToBeep) MyBeep();
  370.  
  371.     ClrAppFiles(count);
  372.  
  373.     return count;
  374. }
  375.  
  376. /***************************************************************************/
  377. void CenterPicRect(PicHandle thePic, Rect *inRect, Rect *outRect)
  378. {
  379.     short        dh,dv;
  380.     
  381.     *inRect = (*thePic)->picFrame;
  382.     dh = (outRect->right - (inRect->right - inRect->left)) / 2;
  383.     dv = (outRect->bottom - (inRect->bottom - inRect->top)) / 2;
  384.     OffsetRect(inRect,dh,dv);
  385. }
  386.  
  387. void   about( void )
  388. {
  389.     DialogPtr    GetSelection;              
  390.     short        itemHit;                       
  391.     short        DType; 
  392.     Handle        DItem;
  393.     GrafPtr        savePort;
  394.  
  395.     Rect        picRect;
  396.     PicHandle    thePict;
  397.  
  398.     GetSelection = GetNewDialog(dlogAbout, NIL, (WindowPtr)-1);
  399.     ShowWindow(GetSelection);           
  400.     SelectWindow(GetSelection);         
  401.     GetPort(&savePort);
  402.     SetPort(GetSelection);
  403.  
  404.  
  405.     GetDItem(GetSelection, 1, &DType, &DItem, &picRect);
  406.     thePict = (PicHandle) Get1Resource('PICT',ABOUT);
  407.     CenterPicRect(thePict, &picRect, &GetSelection->portRect);
  408.     DrawPicture(thePict,&picRect);
  409.  
  410.     ModalDialog(MyFilter, &itemHit);
  411.     EraseRect (&GetSelection->portRect);
  412.     ReleaseResource(thePict);
  413.     
  414.     thePict = (PicHandle) Get1Resource('PICT',GPL);
  415.     CenterPicRect(thePict, &picRect, &GetSelection->portRect);
  416.     DrawPicture(thePict,&picRect);
  417.     
  418.     ModalDialog(MyFilter, &itemHit);
  419.     ReleaseResource(thePict);
  420.  
  421.     DisposDialog(GetSelection);  
  422.     SetPort(savePort);
  423.     
  424. }                                       
  425. static pascal char
  426.             MyFilter (
  427.                         DialogPtr        theDialog,      
  428.                         EventRecord       *theEvent,    
  429.                         short            *itemHit
  430.             )
  431. {
  432.     return ((theEvent->what == keyDown)||(theEvent->what == mouseDown));
  433. }
  434. /***************************************************************************/
  435. void SetUpMenus(void)
  436. {
  437.     short index;
  438.     
  439.     ClearMenuBar();
  440.     
  441.     myMenus[appleM] = GetMenu(appleID);
  442.     AddResMenu(myMenus[appleM], 'DRVR');
  443.     myMenus[fileM] = GetMenu(fileID);
  444.     myMenus[editM] = GetMenu(editID);
  445.     myMenus[gzipM] = GetMenu(gzipID);
  446.     for(index=appleM; index <= gzipM; index++)
  447.         InsertMenu(myMenus[index], 0) ;
  448.  
  449.     CheckItem( myMenus[gzipM], (currPrefs.ascii?gmAscii:gmBin), true);
  450.     CheckItem( myMenus[gzipM], (currPrefs.compress?gmComp:gmUncomp), true);
  451.     
  452.     DrawMenuBar();
  453. }
  454. /***************************************************************************/
  455. void MyBeep(void)
  456. {
  457.     Handle theSound;
  458.     
  459.         theSound = GetResource('snd ', SOUND_ID);
  460.             if (theSound != nil)
  461.             {
  462.                 SndPlay(nil, theSound, false);
  463.                 ReleaseResource(theSound);
  464.             }
  465. }